package com.mrd.gtimpframe.action;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mrd.framework.config.web.json.annotation.JField;
import com.mrd.framework.config.web.json.annotation.Json;
import com.mrd.framework.ui.Jqgrid;
import com.mrd.gtimpframe.entity.AgentRef;
import com.mrd.gtimpframe.entity.TGenComContact;
import com.mrd.gtimpframe.service.AgentRefService;
import com.mrd.gtimpframe.service.CompanyInfoService;
import com.mrd.gtimpframe.service.TGenComContactService;
import com.mrd.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 公司联系人Controller
 * @author han
 */
@Controller
@RequestMapping("/tGenComContact")
public class TGenComContactController {
    @Autowired
    private TGenComContactService tGenComContactService;
    @Autowired
    private CompanyInfoService companyInfoService;
    @Autowired
    private AgentRefService agentRefService;

    @RequestMapping("listPage")
    public String listPage(String agentRefId,Model model) {
        AgentRef agentRef = (AgentRef)agentRefService.selAgentRefAndRecessiveCom(agentRefId);
        model.addAttribute("agentRef",agentRef);
        return "/gtimpframe/orderFor/tGenComContact";
    }

    @RequestMapping("listPageInFrame")
    public String listPageInFrame(String agentRefId,Model model) {
        AgentRef agentRef = (AgentRef)agentRefService.selAgentRefAndRecessiveCom(agentRefId);
        model.addAttribute("agentRef",agentRef);
        return "/gtimpframe/orderFor/tGenComContactInFrame";
    }

    /**
     * 列表
     * @author han
     */
    @RequestMapping("/list")
    @Json({@JField("arefId")})
    public void list(String arefId,Jqgrid jqgrid, HttpServletResponse response) throws Exception {
        String result = "";
        Pageable page = jqgrid.getPage();
        int pageNumber = page.getPageNumber();
        int pageSize = page.getPageSize();
        // 分页
        PageHelper.startPage(pageNumber + 1, pageSize);

        // 排序
        Sort.Order order = jqgrid.getOrder();
        if (order != null) {
            String direction = order.getDirection().toString();
            String propertyName = order.getProperty();
            PageHelper.orderBy(propertyName + " " + direction);
        }
        TGenComContact tGenComContact = FormBeanUtil.getEntityFromJqgrid(jqgrid, TGenComContact.class);
        tGenComContact.setArefId(arefId);
        List<TGenComContact> contactList = tGenComContactService.queryList(tGenComContact);
        PageInfo<TGenComContact> pi = new PageInfo<TGenComContact>(contactList);
        Page<Map<String, Object>> pMap = new PageImpl(contactList, page, pi.getTotal());
        JsonBinder jsonBinder = JsonBinder.buildNormalBinder();
        jsonBinder.setDateFormat("yyyy-MM-dd HH:mm:ss");
        result = jsonBinder.toJson(pMap);
        ResponseUtils.renderJson(response, result);
    }

    /**
     * 保存
     * @author han
     */
    @ResponseBody
    @RequestMapping("/save")
    public R save(TGenComContact tGenComContact) {
        tGenComContact.setCtime(new Date());
        tGenComContact.setCreator(LoginUtil.getLoginAccount());
        tGenComContactService.insert(tGenComContact);
        return R.ok();
    }

    /**
     * 更新
     * @author han
     */
    @ResponseBody
    @RequestMapping("/update")
    public R update(TGenComContact tGenComContact) {
        TGenComContact rtnContact = tGenComContactService.selectByPrimaryKey(tGenComContact.getComContactId());
        tGenComContact.setMtime(new Date());
        tGenComContact.setModifier(LoginUtil.getLoginAccount());
        tGenComContact.setCtime(rtnContact.getCtime());
        tGenComContact.setCreator(rtnContact.getCreator());
        tGenComContact.setDelFlag(rtnContact.getDelFlag());
        tGenComContact.setStatus(rtnContact.getStatus());
        tGenComContactService.updateByPrimaryKey(tGenComContact);
        return R.ok();
    }

    /**
     * 删除
     * @author han
     */
    @ResponseBody
    @RequestMapping("/delete")
    public R delete(@RequestBody String[] ids) {
        tGenComContactService.deleteBatch(ids);
        return R.ok();
    }

    /**
     * 获取联系人列表
     */
    @RequestMapping("/getContact")
    public void getContact(String comInfoId, HttpServletResponse response) throws Exception {
        String agentId = LoginUtil.getLoginUser().getCompany().getId();
        AgentRef agentRef = agentRefService.getAgentRef(agentId, comInfoId);
        this.list(agentRef.getAgentRefId(),new Jqgrid(),response);
    }
}
